package com.huyiyu.dbproject.common.api;

/**
 * 返回通用对象
 */

public class CommonResult<T> {
  private long code;
  private String message;
  private T data;

  public long getCode() {
    return code;
  }

  public void setCode(long code) {
    this.code = code;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  protected CommonResult(){}

  protected CommonResult(long code, String message, T data) {
    this.code = code;
    this.message = message;
    this.data = data;
  }

  /**
   * 返回成功的结果
   * @param data
   * @param <T>
   * @return
   */
  public static <T> CommonResult<T> success(T data){
    return new CommonResult<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
  }

  public static <T> CommonResult<T> success(T data,String message){
    return new CommonResult<T>(ResultCode.SUCCESS.getCode(),message,data);
  }

  public static <T> CommonResult<T> success(){
    return new CommonResult<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),null);
  }

  /**
   * 返回失败的结果
   * @param errorCode
   * @param <T>
   * @return
   */
  public static <T> CommonResult<T> failed(IErrorCode errorCode){
    return new CommonResult<T>(errorCode.getCode(),errorCode.getMessage(),null);
  }

  /**
   * 返回失败的结果
   * @param message
   * @param <T>
   * @return
   */
  public static <T> CommonResult<T> failed(String message){
    return new CommonResult<T>(ResultCode.FAILED.getCode(),message,null);
  }

  /**
   * 参数验证失败返回结果
   */
  public static <T> CommonResult<T> failed(){
    return failed(ResultCode.FAILED);
  }
}
